package com.plm.mr.combiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * KEYIN 输入key:Text
 * VALUEIN 输入value:IntWritable
 * KEYOUT 输出key:Text
 * VALUEOUT 输出value:IntWritable
 */
public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {

    private int sum;
    private IntWritable v = new IntWritable();

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 1累加求和
        // aa <1,1>
        sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }

        // 2输出
        v.set(sum);
        context.write(key, v);
    }
}
